<?php 

class AuthController extends BaseController {
    	
    public function newUser(){
    	$user = new User();
    	if( $user->validate( Input::all() ) ){
    		$user->user_name = Input::get( 'user_name' );
    		$user->email = Input::get( 'email' );
    		$user->password = Hash::make( Input::get( 'password' ) );
    		$user->save();
    	}else
    		return $user->getValidationErrors()->toJSON();
    }
    
    public function login(){
    	$v = Validator::make( Input::all(), array(
    		"user_name" => "required",
    		"password" => "required"
    	) );
    	
    	if( $v->passes() ){
    		$rememberMe = Input::get( 'rememberMe' ) && Input::get( 'rememberMe' ) == '1' ? true : false;
			if( Auth::attempt( array( 'user_name' => Input::get( 'user_name' ), 'password' => Input::get( 'password' ) ), $rememberMe ) )
				return Redirect::to( "profile" );
    		else
				return View::make( 'auth.login', array( 'error' => Lang::get( 'profile.errorLoginAttempt' ) ) );
    	}else
    		return $v->errors()->toJSON();
    }
    
    public function logout(){
    	if( Auth::check() )
    		Auth::logout();
    	
    	return Redirect::to( "/" );
    }
    
}
